package com.mystudy.management.usermanagement.laytablemessageconvert;

import cn.dev33.satoken.secure.SaBase64Util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.mystudy.management.usermanagement.dao.pojo.Admin;
import com.mystudy.management.usermanagement.entity.LoginMessageContainer;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//查询数据解析工具
@Component
public class LayuiSelectMessageConvert {


    /*
    --------------------------------------------登录请求解析---------------------------------------------------------------
     */



    public LoginMessageContainer loginRequestFirstStepMessageConverter(JSONObject requestMessage,String katchaAnswer){
        LoginMessageContainer loginMessageContainer = new LoginMessageContainer();
        String answer  = (requestMessage.getJSONObject("adminLoginInfo")).getString("kaptchaCode");
        System.out.println("用户输入验证码："+answer);
        System.out.println("实际验证码："+katchaAnswer);
        loginMessageContainer.setKaptchaCheck(answer.equalsIgnoreCase(katchaAnswer));
        if(!loginMessageContainer.isKaptchaCheck()){//校验码不正确，不再解析
            return loginMessageContainer;
        }
        loginMessageContainer.setRememberChoice(Boolean.parseBoolean(requestMessage.getJSONObject("adminLoginInfo").getString("choice")));
        Admin admin = new Admin();
        JSONObject requestBody =requestMessage.getJSONObject("adminLoginInfo");
        admin.setAdminId(Integer.parseInt(requestBody.getString("adminID")));
        admin.setAdminPassword(SaBase64Util.decode(requestBody.getString("password")));//密码解密
        loginMessageContainer.setAdmin(admin);
        return loginMessageContainer;

    }





    /*
    ----------------------------------------------访问监管-请求与访问记录-----------------------------------------------------
     */

    //----------------------------------------------访问记录---------------------------------------
    // 查询数据解析
    public Map<String,Object> index2SelectMessageConvert(String indexKind,String indexMessage,String startDate,String endDate) throws ParseException {
        Map<String,Object> result = new HashMap<>();
        //根据解析结果选择查询索引类型
        if(indexKind.equals("byIP")){
            result.put("visitIP",indexMessage);
        }else{
            result.put("userId",Integer.valueOf(indexMessage));
        }
        //将格式化日期转换成业务层处理的Date类型
        result.put("startDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(startDate));
        result.put("endDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(endDate));
        System.out.println("开始日期："+result.get("startDate"));
        System.out.println("结束日期："+result.get("endDate"));
        return result;
    }


    //删除请求数据解析
    public Integer[] index2DeleteMessageConvert(JSONObject deleteMessage){
        JSONArray deleteLogs =deleteMessage.getJSONArray("deleteInfo");
        Integer[] deleteLogIds = new Integer[deleteLogs.size()];
       for(int i =0;i<deleteLogIds.length;i++){
           JSONObject media = deleteLogs.getJSONObject(i);
           deleteLogIds[i] = Integer.valueOf(media.getString("logId"));
       }
        return  deleteLogIds;
    }


    //------------------------------------------------------请求记录-------------------------------------------------------
    //解析查询请求,无日期条件
    public Map<String,Object> index2RequestSelectMessageConvert(String indexMessage) {
        Map<String,Object> result = new HashMap<>();
        result.put("traceIP",indexMessage);
        return result;
    }

    //解析查询请求，有日期条件
    public Map<String,Object> index2RequestSelectMessageConvert(String indexMessage,String startDate,String endDate) throws ParseException {
        Map<String,Object> result = new HashMap<>();
        result.put("traceIP",indexMessage);
        result.put("startDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(startDate));
        result.put("endDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(endDate));
        return result;
    }

    //解析查询请求，只有有日期条件
    public Map<String,Object> index2RequestSelectMessageConvert(String startDate,String endDate) throws ParseException {
        Map<String,Object> result = new HashMap<>();
        result.put("startDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(startDate));
        result.put("endDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(endDate));
        return result;
    }

    //删除请求数据解析
    public Integer[] index2DeleteMessageRequestTraceConvert(JSONObject deleteMessage){
        JSONArray deleteLogs =deleteMessage.getJSONArray("deleteInfo");
        Integer[] deleteLogIds = new Integer[deleteLogs.size()];
        for(int i =0;i<deleteLogIds.length;i++){
            JSONObject media = deleteLogs.getJSONObject(i);
            deleteLogIds[i] = Integer.valueOf(media.getString("traceId"));
        }
        return  deleteLogIds;
    }



/*
-------------------------------------------------------------访问监管-访问控制-------------------------------------------------------
 */

    //查询数据解析
    // 查询数据解析

    //解析Id查询请求
    public Map<String,Object> index2_2SelectMessageConvert(Integer id) {
        Map<String,Object> result = new HashMap<>();
        result.put("userId",id);
        return result;
    }

    //解析名称查询请求
    public Map<String,Object> index2_2SelectMessageConvert(String name) {
        Map<String,Object> result = new HashMap<>();
        result.put("userName",name);
        return result;
    }

    //解析日期查询请求
    public Map<String,Object> index2_2SelectMessageConvert(String startDate,String endDate) throws ParseException {
        Map<String,Object> result = new HashMap<>();
        //将格式化日期转换成业务层处理的Date类型
        result.put("startDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(startDate));
        result.put("endDate",(new SimpleDateFormat("yyyy-MM-dd")).parse(endDate));
        System.out.println("开始日期："+result.get("startDate"));
        System.out.println("结束日期："+result.get("endDate"));
        return result;
    }

    //解析删除请求
    public Integer[] index2_2DeleteMessageConvert(JSONObject deleteMessage){
        JSONArray deleteUsers =deleteMessage.getJSONArray("deleteInfo");
        Integer[] deleteUserIds = new Integer[deleteUsers.size()];
        for(int i =0;i<deleteUserIds.length;i++){
            JSONObject media = deleteUsers.getJSONObject(i);
            deleteUserIds[i] = Integer.valueOf(media.getString("userId"));
        }
        return  deleteUserIds;
    }

    //解析修改请求
    public Map<Integer, Date[]>index2_2ModifyMessageConvert(JSONObject modifyMessage) throws ParseException {
        JSONArray modifyUsers =modifyMessage.getJSONArray("modifyInfo");
        Map<Integer, Date[]> modifyMessages = new HashMap<>();
        for(int i=0;i<modifyUsers.size();i++){
            JSONObject media = modifyUsers.getJSONObject(i);
            Date[] dates = new Date[2];
            dates[0] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(media.getString("startDate"));
            dates[1] =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(media.getString("endDate"));
            modifyMessages.put(Integer.valueOf(media.getString("userId")),dates);

        }
        return modifyMessages;
    }

    // 查询数据解析
    public Map<String,Object> index2_2SelectUnControlMessageConvert(String indexKind,String indexMessage)  {
        Map<String,Object> result = new HashMap<>();
        //根据解析结果选择查询索引类型
        if(indexKind.equals("byName")){
            result.put("userName",indexMessage);
        }else{
            result.put("userId",Integer.valueOf(indexMessage));
        }
        return result;
    }


    //解析添加控制请求
    public Map<Integer, Date[]>index2_2AddMessageConvert(JSONObject addMessage) throws ParseException {
        JSONArray addUsers =addMessage.getJSONArray("addInfo");
        Map<Integer, Date[]> addMessages = new HashMap<>();
        for(int i=0;i<addUsers.size();i++){
            JSONObject media = addUsers.getJSONObject(i);
            Date[] dates = new Date[2];
            dates[0] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(media.getString("startDate"));
            dates[1] =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(media.getString("endDate"));
            addMessages.put(Integer.valueOf(media.getString("userId")),dates);

        }
        return addMessages;
    }

/*
-------------------------------------------------------------访问监管-流量监控------------------------------------------------------
 */

    //解析自定义日期范围
    public Date[] index2_3TraceDatesConvert(JSONObject requestMessage) throws ParseException {
        JSONObject requestData =requestMessage.getJSONObject("selectInfo");
        System.out.println(requestMessage.getJSONObject("selectInfo"));
        Date[] dates = new Date[2];
        dates[0] = new SimpleDateFormat("yyyy-MM-dd").parse(requestData.getString("startDate"));
        dates[1] = new SimpleDateFormat("yyyy-MM-dd").parse(requestData.getString("endDate"));
        return dates;
    }

}